<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreatePayRecordAPIRequest;
use App\Http\Requests\API\UpdatePayRecordAPIRequest;
use App\Models\PayRecord;
use App\Repositories\PayRecordRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class PayRecordController
 * @package App\Http\Controllers\API
 */

class PayRecordAPIController extends AppBaseController
{
    /** @var  PayRecordRepository */
    private $payRecordRepository;

    public function __construct(PayRecordRepository $payRecordRepo)
    {
        $this->payRecordRepository = $payRecordRepo;
    }

    /**
     * Display a listing of the PayRecord.
     * GET|HEAD /payRecords
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->payRecordRepository->pushCriteria(new RequestCriteria($request));
        $this->payRecordRepository->pushCriteria(new LimitOffsetCriteria($request));
        $payRecords = $this->payRecordRepository->all();

        return $this->sendResponse($payRecords->toArray(), 'Pay Records retrieved successfully');
    }

    /**
     * Store a newly created PayRecord in storage.
     * POST /payRecords
     *
     * @param CreatePayRecordAPIRequest $request
     *
     * @return Response
     */
    public function store(CreatePayRecordAPIRequest $request)
    {
        $input = $request->all();

        $payRecords = $this->payRecordRepository->create($input);

        return $this->sendResponse($payRecords->toArray(), 'Pay Record saved successfully');
    }

    /**
     * Display the specified PayRecord.
     * GET|HEAD /payRecords/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var PayRecord $payRecord */
        $payRecord = $this->payRecordRepository->findWithoutFail($id);

        if (empty($payRecord)) {
            return $this->sendError('Pay Record not found');
        }

        return $this->sendResponse($payRecord->toArray(), 'Pay Record retrieved successfully');
    }

    /**
     * Update the specified PayRecord in storage.
     * PUT/PATCH /payRecords/{id}
     *
     * @param  int $id
     * @param UpdatePayRecordAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdatePayRecordAPIRequest $request)
    {
        $input = $request->all();

        /** @var PayRecord $payRecord */
        $payRecord = $this->payRecordRepository->findWithoutFail($id);

        if (empty($payRecord)) {
            return $this->sendError('Pay Record not found');
        }

        $payRecord = $this->payRecordRepository->update($input, $id);

        return $this->sendResponse($payRecord->toArray(), 'PayRecord updated successfully');
    }

    /**
     * Remove the specified PayRecord from storage.
     * DELETE /payRecords/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var PayRecord $payRecord */
        $payRecord = $this->payRecordRepository->findWithoutFail($id);

        if (empty($payRecord)) {
            return $this->sendError('Pay Record not found');
        }

        $payRecord->delete();

        return $this->sendResponse($id, 'Pay Record deleted successfully');
    }
}
